package vn.candyapp.qlct.fragment;

import vn.candyapp.qlct.R;
import vn.candyapp.qlct.adapter.CategoryAdapter;
import vn.candyapp.qlct.adapter.DsTaiKhoanAdapter;
import vn.candyapp.qlct.database.DBAdapter;
import vn.candyapp.qlct.database.model.Account;
import vn.candyapp.qlct.database.model.Category;
import vn.candyapp.qlct.utils.DialogUtils;
import vn.candyapp.qlct.utils.DialogUtils.IDialogConfirmListener;
import vn.candyapp.widget.ActionItem;
import vn.candyapp.widget.QuickAction;
import vn.candyapp.widget.QuickAction.OnActionItemClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class TabCaiDat extends BaseFragment {
	
	public static TabCaiDat newInstance() {
		TabCaiDat f = new TabCaiDat();
		return f;
	}
	
	public static final int ACTION_DELETE = 0x01;
	
	private Spinner spinType, spinAddCat;
	private ArrayAdapter<String> setupItemAdapter;
	private ListView listView;
	private CategoryAdapter dsPhanNhomAdapter;
	private DsTaiKhoanAdapter dsTaiKhoanAdapter;
	private EditText editText;
	private ImageButton btnAdd;
	private DBAdapter dbAdapter;
	private QuickAction quickAction;
	private int mItemClicked = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbAdapter = new DBAdapter(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View v = inflater.inflate(R.layout.tab_caidat, container, false);
		
		setupItemAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.setup_items_names));
		setupItemAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		initWidgets(v);
		addEvents();
		return v;
	}
	
	private void initWidgets(View v) {
		spinType = (Spinner) v.findViewById(R.id.spinType);
		spinType.setAdapter(setupItemAdapter);
		listView = (ListView) v.findViewById(R.id.lvDs);
		dsPhanNhomAdapter = new CategoryAdapter(getActivity());
		dsPhanNhomAdapter.load(new Runnable() {
			
			@Override
			public void run() {
			}
		});
		dsTaiKhoanAdapter = new DsTaiKhoanAdapter(getActivity());
		dsTaiKhoanAdapter.load(new Runnable() {
			
			@Override
			public void run() {
				listView.setAdapter(dsTaiKhoanAdapter);
			}
		});
		editText = (EditText) v.findViewById(R.id.editText1);
		btnAdd = (ImageButton) v.findViewById(R.id.btnAdd);
		spinAddCat = (Spinner) v.findViewById(R.id.spinAddCat);
		ArrayAdapter<String> adapterAddCat = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, new String[] { "Khoản chi", "Khoản thu" });
		adapterAddCat.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		spinAddCat.setAdapter(adapterAddCat);
		
		quickAction = new QuickAction(getActivity(), QuickAction.VERTICAL);
		ActionItem actionDelete = new ActionItem(ACTION_DELETE, "Delete");
		quickAction.addActionItem(actionDelete);
		
	}
	
	private void addEvents() {
		spinType.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position) {
					case 1:
						listView.setAdapter(dsPhanNhomAdapter);
						spinAddCat.setVisibility(View.VISIBLE);
						break;
					default:
						listView.setAdapter(dsTaiKhoanAdapter);
						spinAddCat.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		
		quickAction.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				if (actionId == ACTION_DELETE) {
					if (spinType.getSelectedItemPosition() == 0) {
						DialogUtils.showConfirm(getActivity(), "Bạn có muốn xóa tài khoản này ?", new DialogUtils.IDialogConfirmListener() {

							@Override
							public void onOk() {
								Account acc = (Account) dsTaiKhoanAdapter.getItem(mItemClicked);
								dbAdapter.deleteAccount(acc.getId());
								reloadDsTkAdapter();
							}

							@Override
							public void onCancel() {
							}
						});
						
					} else if (spinType.getSelectedItemPosition() == 1){
						
						DialogUtils.showConfirm(getActivity(), "Bạn có muốn xóa phân nhóm này ?", new DialogUtils.IDialogConfirmListener() {

							@Override
							public void onOk() {
								Category cat = (Category) dsPhanNhomAdapter.getItem(mItemClicked);
								dbAdapter.deleteCategory(cat.getId());
								reloadDsPhanNhomAdapter();
							}

							@Override
							public void onCancel() {
							}
							
						});
						
					}
				}
			}
		});
		
		listView.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mItemClicked = position;
				quickAction.show(view);
			}
		});
		
		btnAdd.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch (spinType.getSelectedItemPosition()) {
					case 0:
						if (!editText.getText().toString().equals("")) {
							dbAdapter.addNewCategory(editText.getText().toString().trim());
							reloadDsTkAdapter();
							editText.setText("");
						} else {
							Toast.makeText(getActivity(), "Vui lòng nhập tên loại Tài khoản!", Toast.LENGTH_SHORT).show();
						}
						break;
					case 1:
						if (!editText.getText().toString().equals("")) {
							int pos = spinAddCat.getSelectedItemPosition() == 1 ? 2 : 1;
							dbAdapter.addCategory(editText.getText().toString().trim(), pos);
							reloadDsPhanNhomAdapter();
							editText.setText("");
						} else {
							Toast.makeText(getActivity(), "Vui lòng nhập tên của phân nhóm!", Toast.LENGTH_SHORT).show();
						}
						break;
					default:
						break;
				}
			}
		});
	}
	
	private void reloadDsTkAdapter() {
		dsTaiKhoanAdapter.load(new Runnable() {
			
			@Override
			public void run() {
				dsTaiKhoanAdapter.notifyDataSetChanged();
			}
		});
	}
	
	private void reloadDsPhanNhomAdapter() {
		dsPhanNhomAdapter.load(new Runnable() {
			
			@Override
			public void run() {
				dsPhanNhomAdapter.notifyDataSetChanged();
			}
		});
	}
}
